package com.estate.service.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.estate.bean.Letter;
import com.estate.mapper.LetterMapper;
import com.estate.service.feign.OwnerFeign;
import com.estate.service.feign.QuartersFeign;
import com.estate.service.service.LetterService;
import com.estate.util.JsonResult;
import com.estate.util.ResultTool;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class LetterServiceImpl extends ServiceImpl<LetterMapper, Letter> implements LetterService {
    @Resource
    private QuartersFeign quartersFeign;
    @Resource
    private OwnerFeign ownerFeign;

    @Override
    public JsonResult listByPage(int page, int size) {
        PageHelper.startPage(page, size);
        List<Letter> list = list();
        list.forEach(e->{
            e.setQuarters(quartersFeign.find(e.getCommunityId()));
            e.setOwner(ownerFeign.find(e.getOwnerId()));
        });
        PageInfo<Letter> pageInfo = new PageInfo<>(list);
        return ResultTool.success(pageInfo);
    }

    @Override
    public JsonResult saveLetter(Letter letter) {
        letter.setStatus(0);
        letter.setIsDelete(0);
        log.info("新增letter数据:{}", letter);
        boolean b = save(letter);
        if (!b) {
            return ResultTool.fail(501, "添加信箱成功");
        }
        return ResultTool.success("success");
    }

    @Override
    public JsonResult deleteLetter(String letterId) {
        log.info("删除的letterId:{}", letterId);
        boolean b = removeById(letterId);
        if (!b) {
            return ResultTool.fail(502, "删除信箱成功");
        }
        return ResultTool.success("success");
    }

    @Override
    public JsonResult updateLetter(Letter letter) {
        log.info("修改letter数据:{}", letter);
        boolean b = updateById(letter);
        if (!b) {
            return ResultTool.fail(503, "修改信箱失败");
        }
        return ResultTool.success("success");
    }
}
